package bbcweb.demo;

import bbcweb.demo.model.QrPayUrlDemo;
import bbcweb.demo.model.QrUrlDemo;
import bbcweb.demo.util.HttpClientUtil;
import bbcweb.demo.util.MD5Util;
import com.google.gson.Gson;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
		try {
			test();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void test() throws Exception  {
		//https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?CCB_IBSVersion=V6&MERCHANTID=105371154110034&POSID=719871000000029286&BRANCHID=370000000&ORDERID=20181101234&PAYMENT=0.01&CURCODE=01&TXCODE=530550&REMARK1=&REMARK2=&RETURNTYPE=3&TIMEOUT=&MAC=4dee277c292581e8a1edc471d3e6fc81
		String bankURL="https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain";
//		bankURL="http://124.127.94.61:8001/CCBIS/ccbMain";
		String MERCHANTID = "105371154110034";
		String POSID = "492382286";
		String BRANCHID = "370000000";
		String ORDERID = "20181120848";
		String PAYMENT= "0.01";
		String CURCODE="01";
		String TXCODE = "530550";//530550     520100
		String REMARK1 = "";
		String REMARK2 = "";
		String RETURNTYPE="3";
		String TIMEOUT = "";
		String PUB32TR2= "e6c24817f2970064c50fde5f020111";

		StringBuffer tmp = new StringBuffer(); //验签字段
		tmp.append("MERCHANTID=");
		tmp.append(MERCHANTID);
		tmp.append("&POSID=");
		tmp.append(POSID);
		tmp.append("&BRANCHID=");
		tmp.append(BRANCHID);
		tmp.append("&ORDERID=");
		tmp.append(ORDERID);
		tmp.append("&PAYMENT=");
		tmp.append(PAYMENT);
		tmp.append("&CURCODE=");
		tmp.append(CURCODE);
		tmp.append("&TXCODE=");
		tmp.append(TXCODE);
		tmp.append("&REMARK1=");
		tmp.append(REMARK1);
		tmp.append("&REMARK2=");
		tmp.append(REMARK2);
		tmp.append("&RETURNTYPE=");
		tmp.append(RETURNTYPE);
		tmp.append("&TIMEOUT=");
		tmp.append(TIMEOUT);
		tmp.append("&PUB=");
		tmp.append(PUB32TR2);

		LinkedHashMap<String,String> map = new LinkedHashMap<>();
		map.put("CCB_IBSVersion","V6");	//必输项
		map.put("MERCHANTID",MERCHANTID);
		map.put("BRANCHID",BRANCHID);
		map.put("POSID",POSID);
		map.put("ORDERID",ORDERID);
		map.put("PAYMENT",PAYMENT);
		map.put("CURCODE",CURCODE);
		map.put("TXCODE",TXCODE);
		map.put("REMARK1",REMARK1);
		map.put("REMARK2",REMARK2);
		map.put("RETURNTYPE",RETURNTYPE);
		map.put("TIMEOUT",TIMEOUT);
		String mac = MD5Util.md5Str("MERCHANTID="+MERCHANTID+"&POSID="+POSID+"&BRANCHID="+BRANCHID+"&ORDERID="+ORDERID+"&PAYMENT=0.01&CURCODE=01&TXCODE=530550&REMARK1=&REMARK2=&RETURNTYPE=3&TIMEOUT=&PUB="+PUB32TR2);
		map.put("MAC", mac);
		System.out.println("MAC: "+mac);
		StringBuilder urlSearch = new StringBuilder(bankURL).append("?CCB_IBSVersion=V6");
		for (Map.Entry<String,String> entry : map.entrySet()) {
			if(!entry.getKey().equals("CCB_IBSVersion"))
				urlSearch.append("&").append(entry.getKey()).append("=").append(entry.getValue());
		}
//		String ret = HttpClientUtil.httpGet(urlSearch.toString(),"UTF-8");//.httpPost(bankURL, null); 	//请求二维码生成链接串
		String ret = HttpClientUtil.httpPost(bankURL, map); 	//请求二维码生成链接串

		System.out.println("first request ret::"+ret);

		Gson gson = new Gson();
		QrPayUrlDemo qrPayUrl = (QrPayUrlDemo) gson.fromJson(ret, QrPayUrlDemo.class);
		System.out.println(qrPayUrl.getPAYURL());
		ret = HttpClientUtil.httpGet(qrPayUrl.getPAYURL(), "UTF-8"); //获取二维码串
		System.out.println("ret::"+ret);
		QrUrlDemo qrUrl = (QrUrlDemo) gson.fromJson(ret, QrUrlDemo.class);
		System.out.println("result::"+ URLDecoder.decode(qrUrl.getQRURL()));

	}
}
